﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Service;

public partial class preventiveView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        txtHiddenPlanningId.Text = Request.QueryString["preventiveId"];
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;

        new Utility().SetRowToDisplay(grdEquipment, Session["rowsToSDisplay"]);
        lblInformation.Text = string.Empty;
        if (!IsPostBack)
        {

            Session["preventiveId"] = Request.QueryString["preventiveId"];
            Session["defaultTabIndex"] = Request.QueryString["tabIndex"];
           
           
                if ((Request.QueryString["detail"] == null))
                {

                }
                else
                {
                    txtHiddenWhereClause.Text = Session["whereClause"].ToString();
                    txthiddenOrderbyClause.Text = Session["orderByClause"].ToString();

                }
            if (Session["pageIndex"] != null)
            {
                grdEquipment.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }
            if ((Request.QueryString["delete"] != null))
            {
                try
                {
                    PreventiveService _preventiveService = new PreventiveService();
                    _preventiveService.DeletePreventive(new Utility().ConvertToInteger(Session["preventiveId"]));
                    lblInformation.Text = string.Empty;

                }
                catch (Exception)
                {
                  //  lblInformation.Text = Resources.Resource.MsgDeletePreventiveFailed;
                }

            }

            txtHiddenPageIndex.Text = "0";
        }
        else txtHiddenPageIndex.Text = "0";
        grdEquipment.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;

    }


    public string GetTaskId()
    {
        string taskString = string.Empty;
        if (Request.QueryString["taskId"] != null)
            taskString = "&taskId=" + Request.QueryString["taskId"];
        return taskString;

    }
    protected void grdEquipment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEquipment.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdEquipment.PageIndex.ToString();
        Session["pageIndex"] = grdEquipment.PageIndex;

    }
    protected void grdEquipment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {

    }
    protected void grdEquipment_SelectedIndexChanged(object sender, EventArgs e)
    {


    }
    protected void grdEquipment_RowDataBound1(object sender, GridViewRowEventArgs e)
    {


    }
    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ? Int32.Parse(Session["pageIndex"].ToString()) : 0;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }
    public string GetDisplayMode()
    {
        return Request.QueryString["detail"];
    }
    protected void btnValidate_Click(object sender, EventArgs e)
    {
        Session["whereClause"] = GetWhereClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;

        
    }
   
    protected string GetWhereClause()
    {
        string whereClause = "";
        string and = " and ";
        string str = "";

        if (drlEmployee.SelectedIndex > 0)
        {
            str += and +" (PREVENTIVEMAINTENANCE.EMPLOYEEID = " + this.drlEmployee.SelectedValue + ")";
            and = " and ";
        }

        
        if(drlMonth.SelectedIndex != 0)
        {
            str += and + "( numberofmonths =" + drlMonth.SelectedValue+" )";
            and = " and ";
            
            
        }
        if (drlEquipmentType.SelectedIndex != 0)
        {
            str += and + "( TYPEEQUIPMENT.TYPEEQUIPEMENTID =" + drlEquipmentType.SelectedValue + " )";
            and = " and ";


        }
       
        if(drlBuilding.SelectedIndex != 0)
        {
            str += and + "(  building.buildingid =" + drlBuilding.SelectedValue + " )";
            and = " and ";
           
        }
        if (txtStartDate.Text != string.Empty)
        {

            str += and + "( STARTDATE  = convert(datetime,'" + txtStartDate.Text + "', 101) )";
            and = " and ";
            
        }
        
                 
        if (!String.IsNullOrEmpty(str))
            whereClause += str + " ";
        return whereClause;
    }


    protected void odsSparepart_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        lblTotalEquipment.Text = "Total Preventive : " +
                                 (e.ReturnValue != null
                                      ? ((DataTable)e.ReturnValue).Rows.Count.ToString()
                                      : 0.ToString());

        grdGroupeSoftware1.Visible = (e.ReturnValue == null
                                      ? true
                                      : ((DataTable)e.ReturnValue).Rows.Count == 0 ? true : false);

    }
    protected void grdEquipment_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Select"))
        {
            int preventiveId = Convert.ToInt32(grdEquipment.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            txtHiddenPlanningId.Text = preventiveId.ToString();
            Session["preventiveId"] = preventiveId;
            Session["defaultTabIndex"] = 1;
             Session["rowSelectedIndex"] = Convert.ToInt32(e.CommandArgument);
        }
        new Utility().SetSelectedColorToGridView(e, grdEquipment);


    }
}
